package com.ls.controller.api;

import com.common.entity.JsonResult;
import com.ls.model.User;
import com.ls.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/2/6.
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private UserService userService;

    @RequestMapping("/test")
    public JsonResult test() {
        redisTemplate.opsForValue().set("test redis", userService.getById(59));
        User user = (User) redisTemplate.opsForValue().get("test redis");
        return new JsonResult(user);
    }

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public JsonResult hello(@RequestBody Map<String, Object> params) throws Exception {
        String name = (String) params.get("name");
        if (name.equals("haha")) throw new Exception("this is a exception!");
        return new JsonResult(name == null ? "default" : name);
    }

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    public JsonResult hello(@PathVariable String name) {
        return new JsonResult(name == null ? "default" : name);
    }

    @RequestMapping(value = "/portal/json", method = RequestMethod.POST)
    public String json() {
        return "{\n" +
                "  \"total\": 1,\n" +
                "  \"rows\": [\n" +
                "    {\n" +
                "      \"productid\": \"FI-SW-01\",\n" +
                "      \"unitcost\": 10.00,\n" +
                "      \"status\": \"P\",\n" +
                "      \"listprice\": 16.50,\n" +
                "      \"attr1\": \"Large\",\n" +
                "      \"itemid\": \"EST-1\"\n" +
                "    }\n" +
                "  ]\n" +
                "}\n";
    }

}
